/**
 * Copyright (c) 2007, Ashish Lijhara
 * All rights reserved.
 *
 * MiniFrameGlass.java
 */

package crazy.java.Aetas;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import java.util.Calendar;
import java.util.Timer;
import java.util.TimerTask;

/**
 *
 * @author Ashish (Java Crazy)
 */

public class MiniFrameGlass extends JComponent{
    
    BufferedImage sun;
    BufferedImage moon;
    BufferedImage pict;
    Calendar a;
    Calendar c;
    JFrame frame;
    Timer timer = new Timer();
    TimerTask clockTask=new TimerTask() {
        public void run() {
            try{
                Thread.sleep(5000);
            }catch(Exception e){
                e.printStackTrace();
            }
            updateTime();
            repaint();
        }
    };
    public MiniFrameGlass(JFrame f){
        frame=f;
        initComponents();
        a=Calendar.getInstance(java.util.TimeZone.getTimeZone("GMT+05:30"));
        timer.schedule(clockTask, 0, 1000);
        c =Calendar.getInstance(java.util.TimeZone.getTimeZone("GMT-06:00"));
        setMinimumSize(new java.awt.Dimension(300, 225));
        setOpaque(false);
    }
    
    private void updateTime() {
        c=Calendar.getInstance(java.util.TimeZone.getTimeZone("GMT-06:00"));
        a=Calendar.getInstance(java.util.TimeZone.getTimeZone("GMT+05:30"));
    }
    
    protected void initComponents(){
        
        JButton jButton1 = new ImageButton(new ImageIcon("res/min_max.png"),new ImageIcon("res/min_max.png"),new ImageIcon("res/min_max_h.png"));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                doMaximizeAnimation();
            }
        });
        jButton1.setToolTipText("Switch To Standard Mode");
        JButton jButton2 = new ImageButton(new ImageIcon("res/min_close.png"),new ImageIcon("res/min_close.png"),new ImageIcon("res/min_close_h.png"));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                System.exit(0);
            }
        });
        jButton2.setToolTipText("Exit");
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(this);
        setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(340, Short.MAX_VALUE)
                .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(1, 1, 1)
                .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
                );
        jPanel1Layout.setVerticalGroup(
                jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                .addComponent(jButton2)
                .addComponent(jButton1))
                .addContainerGap(280, Short.MAX_VALUE))
                );
    }
    
    public void doMaximizeAnimation(){
        AnimationHandler ani=new AnimationHandler(frame);
        ani.maximize();
    }
    
    protected void paintComponent(Graphics g){
        Graphics2D g2d=(Graphics2D)g;
        
        try{
            File file = new File("res/minisun.png");
            sun=ImageIO.read(new URL(file.toURI().toURL().toString()));
        }catch(IOException ioe){
            ioe.printStackTrace();
        }
        
        try{
            File file = new File("res/minimoon.png");
            moon=ImageIO.read(new URL(file.toURI().toURL().toString()));
        }catch(IOException ioe){
            ioe.printStackTrace();
        }
        
        int cahr=c.get(Calendar.HOUR_OF_DAY);
        int inhr=a.get(Calendar.HOUR_OF_DAY);
        if(inhr >=06 && inhr<19){
            g2d.drawImage(sun,240,80,null);
        } else{
            g2d.drawImage(moon,240,80,null);
        }
        if(cahr>=06 && cahr<19){
            g2d.drawImage(sun,240,160,null);
        } else{
            g2d.drawImage(moon,240,160,null);
        }
    }
}
